package design_pattern.ActiveObjects;

import java.awt.font.TextHitInfo;
import java.util.LinkedList;

/**
 * @Author: 蔡翔
 * @Date: 2019/11/27 15:22
 * @Version 1.0
 */
public class ActivationQueue {
    private final static int MAX_METHOD_REQUEST_QUEUE_SIZE = 100;
    //不给它赋值是否原因的，jvm初始化 会很快，用到了才赋值，没用到不赋值。
    private final LinkedList<MethodRequest> methodQuene;

    public ActivationQueue(){
        //如果final 未被赋值，那么这里是可以被赋值的，如果已经被赋值了，这里就不能了。
        methodQuene = new LinkedList<>();
    }

    public synchronized void put(MethodRequest methodRequest){
        while (methodQuene.size()>=MAX_METHOD_REQUEST_QUEUE_SIZE){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.methodQuene.addLast(methodRequest);
    }

    public synchronized MethodRequest take(){
        while (methodQuene.isEmpty()){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        MethodRequest methodRequest = methodQuene.removeFirst();
        this.notifyAll();
        return methodRequest;
    }


}